﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;

namespace Catering.UCPanle
{
    public partial class UCSearchToolWithData : System.Web.UI.UserControl
    {
        #region 属性
        FromRecords frRules = null;
        T_RawMaterials rawMaterialsRule = null;
        List<CheckBox> listCheckBox = null;//存储页面所有CheckBox
        private string[] years;//年份
        private int[] RawId;//食品大类ID
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox2.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-M'})");
                loadData();
            }
        }

        private void loadData()
        {
            rawMaterialsRule = new T_RawMaterials();
            List<Catering_Model.E_RawMaterials> listMaterials = rawMaterialsRule.GetAllList(0);
            Bind_Data_to_Controls.Bind_Material_cbo(listMaterials, this.CheckBoxList1);
        }

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public int[] GetRaw()
        {
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            RawId = new int[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawId[numTemp] = Int32.Parse(CheckBoxList1.Items[i].Value);
                    numTemp += 1;
                }
            }
            return RawId;
        }

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public string[] GetRawName()
        {
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            string[] RawName = new string[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawName[numTemp] = CheckBoxList1.Items[i].Text;
                    numTemp += 1;
                }
            }
            return RawName;
        }

        ///// <summary>
        ///// 是否显示月份
        ///// </summary>
        //public Boolean DisplayMonth
        //{
        //    get
        //    {
        //        return (Boolean)ViewState["allowDuplicates"];
        //    }
        //    set
        //    {
        //        if (!value)
        //        {
        //            Label2.Visible = false;
        //            ddl_Month.Visible = false;
        //        }
        //    }
        //}

        /// <summary>
        /// 是否显示年
        /// </summary>
        public Boolean DisplayYear
        {
            get
            {
                return (Boolean)ViewState["allowDuplicates"];
            }
            set
            {
                Label1.Visible = value;
                TextBox2.Visible = value;
            }
        }

        /// <summary>
        /// 返回已选择时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetDateTime()
        {
            DateTime dTime = DateTime.Parse("1949-10-01");
            if (!string.IsNullOrEmpty(TextBox2.Text))
            {
                DateTime.TryParse(TextBox2.Text, out dTime);
            }
            return dTime;
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CheckBoxList1.Items[0].Selected)
            {
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
            else if (CheckBoxList1.SelectedItem.Text.Equals("全部") && !CheckBoxList1.SelectedItem.Selected && CheckBoxList1.SelectedItem.Value.Equals("-1"))
            {
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = false;
                }
            }
            else
            {
                int selectNum = 0;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        if (selectNum == (CheckBoxList1.Items.Count - 1))
                        {
                            CheckBoxList1.Items[0].Selected = true;
                        }
                        else
                        {
                            CheckBoxList1.Items[0].Selected = false;
                        }
                        selectNum += 1;
                    }
                }
            }
        }

    }
}